/**
 * Created on 2017年1月18日 by caiming
 */
package com.ahdms.es.util;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x509.Certificate;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

/**
 * @Title 
 * @Description 
 * @Copyright <p>Copyright (c) 2016</p>
 * @Company <p>迪曼森信息科技有限公司 Co., Ltd.</p>
 * @author caiming
 * @version 1.0
 * @修改记录
 * @修改序号，修改日期，修改人，修改内容
 */
public class ThriftTool {

	/**
	 * 
	 * @创建人 caiming
	 * @创建时间 2017年1月18日
	 * @创建目的【ByteBuffer转换 byte[]】
	 * @修改目的【修改人：，修改时间：】
	 * @param data
	 * @return
	 */
	public static ByteBuffer byteConvertByteBuffer(byte[] data){
		ByteBuffer byteBuffer = ByteBuffer.wrap(data);
		return byteBuffer;
	}
	
	/**
	 * 
	 * @创建人 caiming
	 * @创建时间 2017年1月18日
	 * @创建目的【byte[]转换 ByteBuffer】
	 * @修改目的【修改人：，修改时间：】
	 * @param data
	 * @return
	 */
	public static byte[] byteBufferConvertByte(ByteBuffer data){
		byte[] bytes = new byte[data.remaining()];
		data.get(bytes, 0, bytes.length);
		data.clear();
		return bytes;
	}
	

	public static byte[] toByteArray(InputStream input) throws IOException {
	    ByteArrayOutputStream output = new ByteArrayOutputStream();
	    byte[] buffer = new byte[4096];
	    int n = 0;
	    while (-1 != (n = input.read(buffer))) {
	        output.write(buffer, 0, n);
	    }
	    return output.toByteArray();
	}
	/**
	 * byte数组转换为ASN1Sequence对象
	 * @param bytes
	 * @param obj
	 */
	public static ASN1Sequence byteToASN1Sequence(byte[] bytes) {
		ASN1InputStream input = null;
		ASN1Primitive object = null;
		if(null!=bytes){
			try {
				input = new ASN1InputStream(bytes);
				object = input.readObject();
			} catch (IOException ioe) {
				ioe.printStackTrace();
				return null;
			} finally {
				try {
					if (null != input) {
						input.close();
					}
				} catch (IOException e) {
					return null;
				}
			}
		}
		return (ASN1Sequence)object;
	}

	public static Certificate getCert(String certPath){
		InputStream in = null;
		Certificate cert = null;
		try {
			//从文件中读取证书数据
			in = new FileInputStream(certPath);
			CertificateFactory certificateFactory = CertificateFactory.getInstance("X509", new BouncyCastleProvider());
			X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(in);

			//将证书byte[]转换成Certificate实体(ASN1)
			ASN1Sequence asn1Sequence = ThriftTool.byteToASN1Sequence(certificate.getEncoded());
			cert = Certificate.getInstance(asn1Sequence);
			System.out.println("证书序列："+cert.getSerialNumber().getPositiveValue().intValue());
			
			
		} catch (Exception ex) {
			return null;
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return cert;
	}
	
}

